package cn.billy.org.service.impl;


import cn.billy.basic.service.impl.BaseServiceImpl;
import cn.billy.basic.utils.MD5Utils;
import cn.billy.basic.utils.StrUtils;
import cn.billy.org.domain.Employee;
import cn.billy.org.domain.Shop;
import cn.billy.org.mapper.EmployeeMapper;
import cn.billy.org.mapper.ShopMapper;
import cn.billy.org.service.IEmployeeService;
import cn.billy.user.domain.Logininfo;
import cn.billy.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @Description: IDepartmentService
* @Author: Billy
* @Version: 1.0
* @Date:  2022/6/21 21:21
*/
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    @Transactional
    public void save(Employee employee) {
        // 获取前端对象输入的简单密码
        String password = employee.getPassword();
        // 随机生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        System.out.println(salt);
        // 调用MD5工具类进行加密
        String byMd5 = MD5Utils.encrypByMd5(salt + password);
        // 将得到的盐值和加密密码设置到employee中
        employee.setSalt(salt);
        employee.setPassword(byMd5);
        // 生成一个新的logininfo对象
        Logininfo logininfo = new Logininfo();
        // 使用BeanUtils工具类将emploee对象复制给logininfo对象
        BeanUtils.copyProperties(employee,logininfo);
        // 调用新增方法，将logininfo对象添加到数据库中
        logininfoMapper.add(logininfo);
        // 获取到logininfo对象的主键id设置到employee的logininfoId字段中
        employee.setLogininfoId(logininfo.getId());
        System.out.println("11111111111111");
        // 调用新增方法
        employeeMapper.add(employee);
        initRelationTableSave(employee);
    }

    public void initRelationTableUpdate(Employee employee){
        if (employee.getId() !=null) {
            employeeMapper.removeEmployeeRole(employee.getId());
            employeeMapper.saveEmployeeRole(employee.getId(),employee.getRoles());
        }
    }

    public void initRelationTableSave(Employee employee){
        if (employee.getId() !=null) {
            employeeMapper.saveEmployeeRole(employee.getId(), employee.getRoles());
        }
    }
    public void initRelationTableReove(Long id){
        employeeMapper.removeEmployeeRole(id);

    }
    @Override
    @Transactional
    public void remove(Long id) {
        Employee employee = employeeMapper.getById(id);
        Long logininfoId = employee.getLogininfoId();
        employeeMapper.remove(id);
        logininfoMapper.remove(logininfoId);
        initRelationTableReove(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        Long logininfoId = employee.getLogininfoId();
        // 查询数据库中的的employee
        Employee nowEmployee = employeeMapper.getById(employee.getId());
        // 获取当前对象的employee对象密码
        String password = employee.getPassword();
        System.out.println("444444"+password);
        // 获取数据库中employee对象的盐值和密码
        String salt = nowEmployee.getSalt();
        String oldPassword = nowEmployee.getPassword();
        System.out.println("33333333"+oldPassword);
//         判断用户是否修改密码
//        String nowPassword = MD5Utils.encrypByMd5(salt + password);
        if (!oldPassword.equals(password)){
            // 随机生成新的盐值
            String newSalt = StrUtils.getComplexRandomString(32);
            // 调用MD5共具类进行加密，然后生成新的加密后的密码
            String newPassword = MD5Utils.encrypByMd5(newSalt + password);
            //将新的盐值和密码设置进去
            employee.setSalt(newSalt);
            employee.setPassword(newPassword);
        }
        // 根据employee的logininfoId查询出数据库中的logininfo对象
        Logininfo logininfo = logininfoMapper.getById(logininfoId);
        System.out.println("1111111"+logininfo);
        System.out.println("2222222"+employee);
        // 将前端传入的employee对象覆盖掉数据库中的logininfo对象
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setId(logininfoId);
        // 将logininfo的类型设置为1，表示是员工
        logininfo.setType(0);
        // 调用修改方法进行修改
        employeeMapper.update(employee);
        logininfoMapper.update(logininfo);
        initRelationTableUpdate(employee);
    }

    @Override
    public Employee loadByLogininfoId(Long logininfoId) {
        return employeeMapper.loadByLogininfoId(logininfoId);
    }

    @Override
    public List<Employee> getByUserId(Long id) {
        Shop byId = shopMapper.getById(id);
        List<Employee> byId1 = employeeMapper.getByEmployee(byId.getAdminId());
        return byId1;
    }


}
